package com.wande.dataplatform.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.wande.common.core.domain.R;
import com.wande.common.log.annotation.Log;
import com.wande.common.log.enums.BusinessType;
import com.wande.common.mybatis.core.page.PageQuery;
import com.wande.common.mybatis.core.page.TableDataInfo;
import com.wande.common.web.core.BaseController;
import com.wande.dataplatform.domain.bo.ProjectBo;
import com.wande.dataplatform.domain.bo.ProjectMemberBo;
import com.wande.dataplatform.domain.dto.ProjectResourceStatisticsDTO;
import com.wande.dataplatform.domain.vo.ProjectMemberVO;
import com.wande.dataplatform.domain.vo.ProjectVO;
import com.wande.dataplatform.service.IProjectService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目管理控制器
 *
 * @author wande
 */
@Validated
@RestController
@RequestMapping("/dataplatform/project")
@RequiredArgsConstructor
public class ProjectController extends BaseController {

    private final IProjectService projectService;

    /**
     * 查询项目列表
     */
    @SaCheckPermission("dataplatform:project:list")
    @GetMapping("/list")
    public TableDataInfo<ProjectVO> list(ProjectBo bo, PageQuery pageQuery) {
        return projectService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询所有项目
     */
    @SaCheckPermission("dataplatform:project:list")
    @GetMapping("/listAll")
    public R<List<ProjectVO>> listAll(ProjectBo bo) {
        return R.ok(projectService.queryList(bo));
    }

    /**
     * 获取项目详情
     */
    @SaCheckPermission("dataplatform:project:query")
    @GetMapping("/{id}")
    public R<ProjectVO> getInfo(@PathVariable Long id) {
        return R.ok(projectService.queryById(id));
    }

    /**
     * 创建项目
     */
    @SaCheckPermission("dataplatform:project:add")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Long> add(@Valid @RequestBody ProjectBo bo) {
        return R.ok(projectService.createProject(bo));
    }

    /**
     * 更新项目
     */
    @SaCheckPermission("dataplatform:project:edit")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Valid @RequestBody ProjectBo bo) {
        projectService.updateProject(bo);
        return R.ok();
    }

    /**
     * 删除项目
     */
    @SaCheckPermission("dataplatform:project:remove")
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> remove(@PathVariable Long id) {
        projectService.deleteProject(id);
        return R.ok();
    }

    /**
     * 归档项目
     */
    @SaCheckPermission("dataplatform:project:edit")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}/archive")
    public R<Void> archive(@PathVariable Long id) {
        projectService.archiveProject(id);
        return R.ok();
    }

    /**
     * 恢复项目
     */
    @SaCheckPermission("dataplatform:project:edit")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}/restore")
    public R<Void> restore(@PathVariable Long id) {
        projectService.restoreProject(id);
        return R.ok();
    }

    /**
     * 查询项目成员列表
     */
    @SaCheckPermission("dataplatform:project:query")
    @GetMapping("/{projectId}/members")
    public R<List<ProjectMemberVO>> listMembers(@PathVariable Long projectId) {
        return R.ok(projectService.queryMemberList(projectId));
    }

    /**
     * 添加项目成员
     */
    @SaCheckPermission("dataplatform:project:edit")
    @Log(title = "项目成员管理", businessType = BusinessType.INSERT)
    @PostMapping("/member")
    public R<Void> addMember(@Valid @RequestBody ProjectMemberBo bo) {
        projectService.addMember(bo);
        return R.ok();
    }

    /**
     * 更新项目成员
     */
    @SaCheckPermission("dataplatform:project:edit")
    @Log(title = "项目成员管理", businessType = BusinessType.UPDATE)
    @PutMapping("/member")
    public R<Void> updateMember(@Valid @RequestBody ProjectMemberBo bo) {
        projectService.updateMember(bo);
        return R.ok();
    }

    /**
     * 删除项目成员
     */
    @SaCheckPermission("dataplatform:project:edit")
    @Log(title = "项目成员管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/member/{id}")
    public R<Void> removeMember(@PathVariable Long id) {
        projectService.deleteMember(id);
        return R.ok();
    }

    /**
     * 获取项目资源统计
     */
    @SaCheckPermission("dataplatform:project:query")
    @GetMapping("/{projectId}/statistics")
    public R<ProjectResourceStatisticsDTO> getStatistics(@PathVariable Long projectId) {
        return R.ok(projectService.getResourceStatistics(projectId));
    }
}
